from flask import Flask
from config import config
import py_eureka_client.eureka_client as eureka_client


# 跨域请求
# def after_request(resp):
#     resp.headers['Access-Control-Allow-Origin'] = '*'
#     return resp


def create_app(config_filename):
    # 1.创建应用
    app = Flask(__name__)
    # 2 配置文件
    app.config.from_object(config[config_filename])

    from . import api
    api.init_app(app)

    from app import repository
    repository.init_app(app)

    your_rest_server_host = "192.168.10.106"
    your_rest_server_port = 9090
    # The flowing code will register your server to eureka server and also start to send heartbeat every 30 seconds
    eureka_client.init(eureka_server="http://192.168.76.10:8001/eureka",
                       app_name="ubo-ai",
                       # 当前组件的主机名，可选参数，如果不填写会自动计算一个，如果服务和 eureka 服务器部署在同一台机器，请必须填写，否则会计算出 127.0.0.1
                       # instance_host=your_rest_server_host,
                       instance_port=8080,
                       # 调用其他服务时的高可用策略，可选，默认为随机
                       ha_strategy=eureka_client.HA_STRATEGY_RANDOM)
    return app;
